package com.kfh.ybracelet.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.kfh.ybracelet.R;
import com.kfh.ybracelet.bean.BabyDetailsResult;
import com.kfh.ybracelet.my.BabyDetailsActivity;
import com.kfh.ybracelet.net.HttpUtil;
import com.kfh.ybracelet.utils.CommonUtils;
import com.kfh.ybracelet.utils.Constant;
import com.kfh.ybracelet.widget.CircleImageView;

import java.util.List;


/**
 * 关注老人的列表的adapter
 * Created by Administrator on 2015/9/21.
 */
public class AboutBabyCountAdapter extends BaseAdapter {

    private List<BabyDetailsResult.KeepEntity> aboutDatas;
    private Context ctx;
    protected SharedPreferences sp;
    private int myLevel;//我的等级
    private String childId;//当前孩子的id

    @Deprecated
    public AboutBabyCountAdapter(Context ctx, List<BabyDetailsResult.KeepEntity> aboutDatas, String childId) {
        this.ctx = ctx;
        this.aboutDatas = aboutDatas;
        this.childId = childId;
        sp = ctx.getSharedPreferences(Constant.CONFIG, Context.MODE_PRIVATE);
    }

    public void setData(List<BabyDetailsResult.KeepEntity> aboutDatas) {
        this.aboutDatas = aboutDatas;
    }

    public void setMyLevel(int myLevel) {
        this.myLevel = myLevel;
    }

    @Override
    public int getCount() {
        return aboutDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (convertView == null) {
            convertView = View.inflate(ctx, R.layout.items_babyabout, null);
            viewHolder = new ViewHolder();
            viewHolder.tv2_baby_name = (TextView) convertView.findViewById(R.id.tv2_baby_name);
            viewHolder.tv2_grade = (TextView) convertView.findViewById(R.id.tv2_grade);
            viewHolder.tv_telenphone = (TextView) convertView.findViewById(R.id.tv_telenphone);
            viewHolder.iv_upgrade = (TextView) convertView.findViewById(R.id.top_arrow);
            viewHolder.iv_degrade = (TextView) convertView.findViewById(R.id.bottom_arrow);
            viewHolder.iv_delete = (TextView) convertView.findViewById(R.id.icon_slide_delete);
            viewHolder.iv_baby2_photo = (CircleImageView) convertView.findViewById(R.id.iv_baby2_photo);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv2_baby_name.setText(aboutDatas.get(position).name);

        switch (myLevel) {

            case Constant.POWER_ADMIN://我是管理员
                //管理员   可以对管理员降级(不包括自己) 可以删除自己,也可以删除别的管理员  可以对 亲人 升级和 降级   可以对朋友删除和升级

                switch (aboutDatas.get(position).level) {
                    //对方的权限
                    case Constant.POWER_ADMIN://管理员等级

                        //无法升职
                        viewHolder.iv_upgrade.setVisibility(View.INVISIBLE);
                        //可以降职和删除
                        viewHolder.iv_delete.setVisibility(View.VISIBLE);
//                        viewHolder.iv_degrade.setVisibility(View.VISIBLE);

                        //如果是自己 则不能降自己
                        if (sp.getString(Constant.USERID, "").equals(aboutDatas.get(position).phoneNum)) {
                            viewHolder.iv_degrade.setVisibility(View.INVISIBLE);
                        } else {
                            viewHolder.iv_degrade.setVisibility(View.VISIBLE);
                        }

                        break;

                    case Constant.POWER_PARENT://亲人等级
                        //可升 可降, 可删
                        viewHolder.iv_upgrade.setVisibility(View.VISIBLE);
                        viewHolder.iv_degrade.setVisibility(View.VISIBLE);
                        viewHolder.iv_delete.setVisibility(View.VISIBLE);
                        break;

                    case Constant.POWER_FRIEND://朋友等级
                        //可删 可升  不可降级
                        viewHolder.iv_upgrade.setVisibility(View.VISIBLE);
                        viewHolder.iv_delete.setVisibility(View.VISIBLE);
                        viewHolder.iv_degrade.setVisibility(View.INVISIBLE);
                        break;

                    case Constant.POWER_TEACHER://老师等级
                        viewHolder.iv_upgrade.setVisibility(View.INVISIBLE);
                        viewHolder.iv_delete.setVisibility(View.INVISIBLE);
                        viewHolder.iv_degrade.setVisibility(View.INVISIBLE);
                        break;

                    default:
                        viewHolder.iv_upgrade.setVisibility(View.INVISIBLE);
                        viewHolder.iv_delete.setVisibility(View.INVISIBLE);
                        viewHolder.iv_degrade.setVisibility(View.INVISIBLE);
                        break;

                }

                break;

            case Constant.POWER_PARENT://我是亲人
                //亲人 没权限可以操作 但是可以删除自己
                viewHolder.iv_upgrade.setVisibility(View.INVISIBLE);
                viewHolder.iv_degrade.setVisibility(View.INVISIBLE);

                if (Constant.id.equals(aboutDatas.get(position).id)) {
                    viewHolder.iv_delete.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.iv_delete.setVisibility(View.INVISIBLE);
                }

                break;

            case Constant.POWER_FRIEND://我是朋友等级
                //朋友等级  可以删除自己

                if (Constant.id.equals(aboutDatas.get(position).id)) {
                    viewHolder.iv_delete.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.iv_delete.setVisibility(View.INVISIBLE);
                }
                viewHolder.iv_upgrade.setVisibility(View.INVISIBLE);
                viewHolder.iv_degrade.setVisibility(View.INVISIBLE);

                break;

            case Constant.POWER_TEACHER://我是老师等级
                viewHolder.iv_upgrade.setVisibility(View.INVISIBLE);
                viewHolder.iv_delete.setVisibility(View.INVISIBLE);
                viewHolder.iv_degrade.setVisibility(View.INVISIBLE);
                break;

            case Constant.POWER_DEL://我是 被删除用户
                viewHolder.iv_upgrade.setVisibility(View.INVISIBLE);
                viewHolder.iv_delete.setVisibility(View.INVISIBLE);
                viewHolder.iv_degrade.setVisibility(View.INVISIBLE);
                break;
            default:
                break;

        }

        //是自己, 可以删除自己
        if (sp.getString(Constant.USERID, "").equals(aboutDatas.get(position).phoneNum)) {
            viewHolder.iv_delete.setVisibility(View.VISIBLE);
        }

        //设置权限数据
        switch (aboutDatas.get(position).level) {

            case Constant.POWER_ADMIN:
                viewHolder.tv2_grade.setText("管理员等级");

                break;

            case Constant.POWER_PARENT:
                viewHolder.tv2_grade.setText("亲人等级");

                break;

            case Constant.POWER_FRIEND:
                viewHolder.tv2_grade.setText("朋友等级");

                break;

            case Constant.POWER_TEACHER:
                viewHolder.tv2_grade.setText("老师等级");
                break;

            default:
                viewHolder.tv2_grade.setText("");
                break;

        }

//        if ("1".equals(aboutDatas.get(position).level)) {
//
//        } else if ("2".equals(aboutDatas.get(position).level)) {
//
//            viewHolder.tv2_grade.setText("亲人等级");
//
//        } else if ("3".equals(aboutDatas.get(position).level)) {
//
//            viewHolder.tv2_grade.setText("朋友等级");
//        } else if ("10".equals(aboutDatas.get(position).level)) {
//            viewHolder.tv2_grade.setText("老师等级");
//        } else {
//            viewHolder.tv2_grade.setText("");
//        }

        viewHolder.tv_telenphone.setText("(" + aboutDatas.get(position).phoneNum + ")");

        Glide.with(ctx)
                .load(aboutDatas.get(position).imgUrl)
                .dontAnimate().diskCacheStrategy(DiskCacheStrategy.SOURCE).fitCenter()
                .placeholder(R.mipmap.temp_pic)
                .into(viewHolder.iv_baby2_photo);

        //升级
        viewHolder.iv_upgrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch (aboutDatas.get(position).level) {
                    case Constant.POWER_PARENT://如果是亲人  升级升为管理员
                        levelAdjust(childId, Constant.POWER_ADMIN, aboutDatas.get(position).id);
                        break;

                    case Constant.POWER_FRIEND: //如果是朋友等级, 升级为亲人
                        levelAdjust(childId, Constant.POWER_PARENT, aboutDatas.get(position).id);
                        break;
                    default:
                        Toast.makeText(ctx, "无法升级", Toast.LENGTH_SHORT).show();
                        break;
                }

            }
        });

        //降级
        viewHolder.iv_degrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch (aboutDatas.get(position).level) {

                    case Constant.POWER_ADMIN: //如果是管理员 , 降为 亲人
                        levelAdjust(childId, Constant.POWER_PARENT, aboutDatas.get(position).id);
                        break;
                    case Constant.POWER_PARENT: //如果是亲人, 降为朋友
                        levelAdjust(childId, Constant.POWER_FRIEND, aboutDatas.get(position).id);
                        break;
                }

            }
        });

        //删除
        viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                //判断当前点击删除的按钮是否是自己, 如果是自己删除自己则调用 delChild, 否则调用 updatelevel接口

                if (sp.getString(Constant.USERID, "").equals(aboutDatas.get(position).phoneNum)) {

                    //点击的是删除自己
//                    LogUtil.e("tag", "删除自己");

                    new AlertDialog.Builder(ctx).setMessage("确定删除该老人").setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).setPositiveButton("删除", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            dialog.dismiss();

                            delArea();

                        }
                    }).show();

                } else {

                    //点击的是删除别人
                    new AlertDialog.Builder(ctx).setMessage("确定删除用户 " + aboutDatas.get(position).name).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).setPositiveButton("删除", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            dialog.dismiss();

                            levelAdjust(childId, Constant.POWER_DEL, aboutDatas.get(position).id);

                        }
                    }).show();

                }

            }
        });
        return convertView;
    }

    //删除老人
    private void delArea() {

        JSONObject j = new JSONObject();

        j.put("id", childId);
        j.put("token", sp.getString(Constant.TOKEN, ""));

        HttpUtil.setPostRequest("加载中...", ctx, Constant.WEB_ROOT + Constant.DELCHILD, j.toJSONString(),
                BabyDetailsActivity.class.getName(), new HttpUtil.SuccessListener() {

                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            org.json.JSONObject j = new org.json.JSONObject(str);

//                            if (j.getInt("code") == Constant.STATUS_SUCCESS) {
                            Toast.makeText(ctx, "删除成功", Toast.LENGTH_SHORT).show();
//                                showToast("删除成功");

                            //看看当前选择的是否是被删除的 如果是则删除sp中缓存的数据
                            if (sp.getString(Constant.CURRENTCHILDID, "").equals(childId)) {
                                SharedPreferences.Editor e = sp.edit();
                                e.putString(Constant.CURRENTCHILDID, "");
                                e.putString(Constant.CURRENTCHILDNAME, "");
                                e.putInt(Constant.LEVEL, -1);
                                e.apply();
                            }

                            if (ctx instanceof BabyDetailsActivity) {
                                ((BabyDetailsActivity) ctx).finish();
                            }

//                            } else {
//                                Toast.makeText(ctx, Constant.showErrorMsg(j.getInt("code"), ctx), Toast.LENGTH_SHORT).show();
//                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, null);

    }

    private class ViewHolder {

        private TextView tv2_baby_name;
        private TextView tv2_grade;
        private TextView tv_telenphone;
        private TextView iv_upgrade;
        private TextView iv_degrade;
        private TextView iv_delete;
        private CircleImageView iv_baby2_photo;

    }

    /**
     * 权限请求, 升级 降级 删除
     *
     * @param id     孩子的id
     * @param level  被调整的等级
     * @param userId 被调整人的id
     */
    public void levelAdjust(String id, int level, String userId) {

        JSONObject j = new JSONObject();
        j.put("id", id);//孩子的id
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("level", level);
        j.put("userId", userId);//被提示或者降级的用户id

        HttpUtil.setPostRequest("加载中...", ctx, Constant.WEB_ROOT + Constant.UPDATELEVEL, j.toJSONString(), BabyDetailsActivity.class.getName(), new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                if (CommonUtils.isNull(str)) {
                    Toast.makeText(ctx, Constant.NODATA, Toast.LENGTH_SHORT).show();
                }

                try {

//                    org.json.JSONObject j = new org.json.JSONObject(str);

//                    if (j.getInt("code") == Constant.STATUS_SUCCESS) {

                    Toast.makeText(ctx, "更改成功!", Toast.LENGTH_SHORT).show();

                    //权限修改成功后需刷新页面
                    if (ctx instanceof BabyDetailsActivity)
                        ((BabyDetailsActivity) ctx).getData();

//                    } else {
//                        Toast.makeText(ctx, Constant.showErrorMsg(j.getInt("code"), ctx), Toast.LENGTH_SHORT).show();
//                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(ctx, Constant.NODATA, Toast.LENGTH_SHORT).show();
                }

            }

        }, null);

    }

}